package com.example.bilibili;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.Call;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.example.bilibili.home.TuijianFragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

public class SousuoActivity extends AppCompatActivity {

    private ResultBeanData.ResultBean resultBean;
    private String Sguanjianci;
    private String Sexist = "NULL";
    private String Shot;
    private String Stitle;
    private String SId;
    private String Sbofangliang;
    private String Sred;
    private int finali;

    private EditText sousuokuang;
    private ImageView iv_hot;
    private TextView tv_title;
    private TextView tv_red;
    private TextView Id;
    private TextView bofangliang;
    private TextView quxiao;
    private TextView sousuo2;
    private LinearLayout shang;
    private ImageView nofind;
    private static final String TAG = TuijianFragment.class.getSimpleName();
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_sousuo);
        initdata();
    }


    private void initdata() {
        Log.e(TAG, "搜索数据被初始化了");
        //联网请求主页的数据
        getDataFromNet();
        sousuokuang = (EditText) findViewById( R.id.sousuokuang );
        iv_hot = (ImageView) findViewById( R.id.iv_hot );
        tv_title = (TextView) findViewById( R.id.tv_title );
        tv_red = (TextView) findViewById( R.id.tv_red );
        Id = (TextView) findViewById( R.id.Id );
        quxiao = (TextView) findViewById( R.id.quxiao );
        bofangliang = (TextView) findViewById( R.id.bofangliang );
        sousuo2 = (TextView) findViewById( R.id.sousuo2 );
        shang = (LinearLayout) findViewById( R.id.shang );
        nofind = (ImageView) findViewById( R.id.nofind );
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void getDataFromNet() {
        String url = Constants.HOME_URL;
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(new StringCallback() {

                    @Override
                    public void onError(Call call, Exception e, int id) {

                        Log.e(TAG,"搜索请求失败=="+e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e(TAG,"搜索请求成功==");
                        //解析数据
                        processData(response);
                    }


                });
    }

    private void processData(String json) {
        ResultBeanData resultBeanData = JSON.parseObject(json,ResultBeanData.class);
        resultBean = resultBeanData.getResult();
        if(resultBean != null){
            Sguanjianci = (String) getIntent().getStringExtra("Sguanjianci");
            Log.e(TAG,"Sguanjianci=="+Sguanjianci);

            for (int i = 0; i < resultBean.getHot_info().size(); i++){
                Log.e(TAG,"getName()=="+i+resultBean.getHot_info().get(i).getName());
                Log.e(TAG,"contains=="+i+resultBean.getHot_info().get(i).getName().contains(Sguanjianci));
                if(resultBean.getHot_info().get(i).getName().contains(Sguanjianci)){
                    Sexist="Y";
                    Shot=resultBean.getHot_info().get(i).getFigure();
                    Stitle=resultBean.getHot_info().get(i).getName();
                    SId=resultBean.getHot_info().get(i).getId();
                    Sbofangliang=resultBean.getHot_info().get(i).getBofangliang();
                    Sred=resultBean.getHot_info().get(i).getRed();
                    finali = i;
                    break;
                }
            }

                Log.e(TAG,"Stitle=="+Stitle);
                Log.e(TAG,"Sexist=="+Sexist);

                if(Sexist.length()==1){
                    Log.e(TAG,"收到数据了"+Sexist);

                    //设置关键词的颜色为粉红
                    SpannableStringBuilder style = new SpannableStringBuilder(Stitle);
                    style.setSpan(new ForegroundColorSpan( getResources().getColor(R.color.bilipink) ), Stitle.indexOf(Sguanjianci), Stitle.indexOf(Sguanjianci)+Sguanjianci.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_title.setText(style);

                    shang.setVisibility(View.VISIBLE);
                    nofind.setVisibility(View.GONE);
                    sousuokuang.setHint(Sguanjianci);
                    Glide.with(SousuoActivity.this).load(Constants.BASE_IMAGE_URL+Shot).into(iv_hot);
                    tv_red.setText(Sred);
                    Id.setText(SId);
                    bofangliang.setText(Sbofangliang);

                    shang.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ResultBeanData.ResultBean.HotInfoBean hotInfoBean =  resultBean.getHot_info().get(finali);
                            GoodsBean goodsBean = new GoodsBean();
                            goodsBean.setFensi(hotInfoBean.getFensi());
                            goodsBean.setBofangliang(hotInfoBean.getBofangliang());
                            goodsBean.setCommennt(hotInfoBean.getCommennt());
                            goodsBean.setFensi(hotInfoBean.getFigure());
                            goodsBean.setName(hotInfoBean.getName());
                            goodsBean.setTime(hotInfoBean.getTime());
                            goodsBean.setRed(hotInfoBean.getRed());
                            goodsBean.setTouxiang(hotInfoBean.getTouxiang());
                            goodsBean.setShijian(hotInfoBean.getShijian());
                            goodsBean.setFensi(hotInfoBean.getFensi());
                            goodsBean.setDianzan(hotInfoBean.getDianzan());
                            goodsBean.setToubi(hotInfoBean.getToubi());
                            goodsBean.setShoucang(hotInfoBean.getShoucang());
                            goodsBean.setZhuanfa(hotInfoBean.getZhuanfa());
                            goodsBean.setShipin(hotInfoBean.getShipin());
                            goodsBean.setId(hotInfoBean.getId());
                            goodsBean.setC1(hotInfoBean.getC1());
                            goodsBean.setC2(hotInfoBean.getC2());
                            goodsBean.setC3(hotInfoBean.getC3());
                            goodsBean.setHotposition(hotInfoBean.getHotposition());
                            Intent intent = new Intent(SousuoActivity.this, GoodsInfoActivity.class);
                            intent.putExtra("GOODS_BEAN",goodsBean);
                            SousuoActivity.this.startActivity(intent);
                        }
                    });
                }else {
                    Log.e(TAG,"没收到数据"+Sexist);
                    nofind.setVisibility(View.VISIBLE);
                    shang.setVisibility(View.GONE);
                }
                sousuokuang.setHint(Sguanjianci);

                //二级界面的搜索功能
            sousuo2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Sguanjianci = sousuokuang.getText().toString();
                    for (int i = 0; i < resultBean.getHot_info().size(); i++){
                        Log.e(TAG,"getName()=="+i+resultBean.getHot_info().get(i).getName());
                        Log.e(TAG,"contains=="+i+resultBean.getHot_info().get(i).getName().contains(Sguanjianci));
                        if(resultBean.getHot_info().get(i).getName().contains(Sguanjianci)){
                            Sexist="Y";
                            Shot=resultBean.getHot_info().get(i).getFigure();
                            Stitle=resultBean.getHot_info().get(i).getName();
                            SId=resultBean.getHot_info().get(i).getId();
                            Sbofangliang=resultBean.getHot_info().get(i).getBofangliang();
                            Sred=resultBean.getHot_info().get(i).getRed();
                            finali = i;
                            break;
                        }
                    }

                    Log.e(TAG,"Stitle=="+Stitle);
                    Log.e(TAG,"Sexist=="+Sexist);

                    if(Sexist.length()==1){
                        Log.e(TAG,"收到数据了"+Sexist);

                        //设置关键词的颜色为粉红
                        SpannableStringBuilder style = new SpannableStringBuilder(Stitle);
                        style.setSpan(new ForegroundColorSpan( getResources().getColor(R.color.bilipink) ), Stitle.indexOf(Sguanjianci), Stitle.indexOf(Sguanjianci)+Sguanjianci.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        tv_title.setText(style);

                        shang.setVisibility(View.VISIBLE);
                        nofind.setVisibility(View.GONE);
                        sousuokuang.setHint(Sguanjianci);
                        Glide.with(SousuoActivity.this).load(Constants.BASE_IMAGE_URL+Shot).into(iv_hot);
                        tv_red.setText(Sred);
                        Id.setText(SId);
                        bofangliang.setText(Sbofangliang);

                        shang.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                ResultBeanData.ResultBean.HotInfoBean hotInfoBean =  resultBean.getHot_info().get(finali);
                                GoodsBean goodsBean = new GoodsBean();
                                goodsBean.setFensi(hotInfoBean.getFensi());
                                goodsBean.setBofangliang(hotInfoBean.getBofangliang());
                                goodsBean.setCommennt(hotInfoBean.getCommennt());
                                goodsBean.setFensi(hotInfoBean.getFigure());
                                goodsBean.setName(hotInfoBean.getName());
                                goodsBean.setTime(hotInfoBean.getTime());
                                goodsBean.setRed(hotInfoBean.getRed());
                                goodsBean.setTouxiang(hotInfoBean.getTouxiang());
                                goodsBean.setShijian(hotInfoBean.getShijian());
                                goodsBean.setFensi(hotInfoBean.getFensi());
                                goodsBean.setDianzan(hotInfoBean.getDianzan());
                                goodsBean.setToubi(hotInfoBean.getToubi());
                                goodsBean.setShoucang(hotInfoBean.getShoucang());
                                goodsBean.setZhuanfa(hotInfoBean.getZhuanfa());
                                goodsBean.setShipin(hotInfoBean.getShipin());
                                goodsBean.setId(hotInfoBean.getId());
                                goodsBean.setC1(hotInfoBean.getC1());
                                goodsBean.setC2(hotInfoBean.getC2());
                                goodsBean.setC3(hotInfoBean.getC3());
                                goodsBean.setHotposition(hotInfoBean.getHotposition());
                                Intent intent = new Intent(SousuoActivity.this, GoodsInfoActivity.class);
                                intent.putExtra("GOODS_BEAN",goodsBean);
                                SousuoActivity.this.startActivity(intent);
                            }
                        });
                    }else {
                        Log.e(TAG,"没收到数据"+Sexist);
                        nofind.setVisibility(View.VISIBLE);
                        shang.setVisibility(View.GONE);
                    }

                }
            });

        }
        else{
            //没有数据
        }
    }

}